<template>
	<el-dialog v-model="visible" :title="handleType=='receipt' ? '入库' : '修改' " :close-on-click-modal='false'
		:close-on-press-escape='false' :fullscreen='true'>
		<el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataFormRef"
			@keyup.enter.native="dataFormSubmitHandle()" label-width="120px">

			<el-form-item label="入库单号" prop="receiptkey">
				<el-input v-model="dataForm.receiptkey" placeholder="入库单号" disabled></el-input>
			</el-form-item>

			<el-form-item label="明细行号" prop="line">
				<el-input v-model="dataForm.line" placeholder="明细行号" disabled></el-input>
			</el-form-item>

			<el-form-item label="状态" prop="status">
				<!-- <el-input v-model="dataForm.status" placeholder="状态" disabled></el-input> -->
				<el-select v-model="dataForm.status" placeholder="请选择" disabled>
					<el-option v-for="item in receiptStatus" :key="item.list" :label="item.listname"
						:value="reString(item.list)">
					</el-option>
				</el-select>
			</el-form-item>

			<el-form-item v-if="handleType!='receipt'" label="明细记录行号" prop="linelist">
				<el-input v-model="dataForm.linelist" placeholder="明细记录行号" disabled></el-input>
			</el-form-item>

			<el-form-item label="物料编码" prop="sku">
				<el-input v-model="dataForm.sku" placeholder="物料编码" disabled></el-input>
			</el-form-item>

			<el-form-item label="物料名称" prop="sku">
				<el-input v-model="dataForm.descr" disabled></el-input>
			</el-form-item>

			<el-form-item label="计量编码/工装号" prop="factorycdoe">
				<el-input v-model="dataForm.factorycdoe" disabled >
				</el-input>
			</el-form-item>

			<el-form-item label="单位" prop="unit" disabled>
				<el-input v-model="dataForm.unit" placeholder="单位" disabled></el-input>
			</el-form-item>

			<el-form-item label="包装" prop="pack">
				<el-input v-model="dataForm.pack" placeholder="包装" disabled></el-input>
			</el-form-item>

			<el-form-item label="预计数量" prop="qty">
				<el-input v-model="dataForm.qty" placeholder="预计数量" disabled></el-input>
			</el-form-item>

			<el-form-item label="已收数量" prop="receiptqty">
				<el-input v-model="dataForm.receiptqty" placeholder="已收数量" disabled></el-input>
			</el-form-item>

			<el-form-item label="数量" prop="actqty">
				<el-input v-model="dataForm.actqty" placeholder="数量" onkeyup="value=value.replace(/[^0-9.]/g,'')"></el-input>
			</el-form-item>

			<el-form-item label="目的库位" prop="toloc">
				<el-input v-model="dataForm.toloc" placeholder="目的库位" :disabled="handleType!='receipt'">
				</el-input>
			</el-form-item>

			<el-form-item label="目的箱" prop="totray">
				<el-input v-model="dataForm.totray" placeholder="目的箱" :disabled="handleType!='receipt'">
				</el-input>
			</el-form-item>

			<el-form-item label="目的格" prop="tobox">
				<el-select v-model="dataForm.tobox" placeholder="请选择">
					<el-option v-for="item in boxs" :key="item.list" :label="item.listname" :value="item.list">
					</el-option>
				</el-select>
			</el-form-item>

			<!-- <el-form-item label="批号" prop="lot">
            <el-input v-model="dataForm.lot" placeholder="批号"></el-input>
          </el-form-item> -->

			<el-form-item label="生产日期" prop="lot1">
				<el-date-picker v-model="dataForm.lot1" type="date" placeholder="生产日期" format="YYYY-MM-DD">
				</el-date-picker>
			</el-form-item>

			<el-form-item label="有效日期" prop="lot2">
				<el-date-picker v-model="dataForm.lot2" type="date" placeholder="有效日期" format="YYYY-MM-DD">
				</el-date-picker>
			</el-form-item>

			<el-form-item label="是否加长" prop="lot3">
				<el-select v-model="dataForm.lot3">
					<el-option value="" label="--"></el-option>
					<el-option v-for="item in yesornos" :key="item.list" :label="item.listname" :value="item.list">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="价格" prop="lot4">
				<el-input v-model="dataForm.lot4" placeholder="价格"></el-input>
			</el-form-item>
			<el-form-item label="部门/供应商" prop="lot5">
				<el-input v-model="dataForm.lot5" placeholder="部门/供应商"></el-input>
			</el-form-item>
			<el-form-item label="计划单号/工序号" prop="lot6">
				<el-input v-model="dataForm.lot6" placeholder="计划单号/工序号"></el-input>
			</el-form-item>
			<el-form-item label="主制车间" prop="lot7">
				<el-input v-model="dataForm.lot7" placeholder="主制车间"></el-input>
			</el-form-item>
			<el-form-item label="任务编码" prop="lot8">
				<el-input v-model="dataForm.lot8" placeholder="任务编码"></el-input>
			</el-form-item>
			<el-form-item label="新旧" prop="lot9">
				<!-- <el-input v-model="dataForm.lot9" placeholder="新旧"></el-input> -->
				<el-select v-model="dataForm.lot9">
					<el-option value="" label="--"></el-option>
					<el-option v-for="item in newandolds" :key="item.list" :label="item.listname" :value="item.list">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="作废" prop="lot10">
				<el-select v-model="dataForm.lot10">
					<el-option value="" label="--"></el-option>
					<el-option v-for="item in yesornos" :key="item.list" :label="item.listname" :value="item.list">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="炉批号" prop="lot11">
				<el-input v-model="dataForm.lot11" placeholder="炉批号"></el-input>
			</el-form-item>
			<el-form-item label="材料复验号" prop="lot12">
				<el-input v-model="dataForm.lot12" placeholder="材料复验号"></el-input>
			</el-form-item>
			<el-form-item label="牌号" prop="lot13">
				<el-input v-model="dataForm.lot13" placeholder="牌号"></el-input>
			</el-form-item>
			<el-form-item label="属性14" prop="lot14">
				<el-input v-model="dataForm.lot14" placeholder="属性14"></el-input>
			</el-form-item>
			<el-form-item label="属性15" prop="lot15">
				<el-input v-model="dataForm.lot15" placeholder="属性15"></el-input>
			</el-form-item>

			<el-form-item label="备注" prop="remark">
				<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
			</el-form-item>

			<el-form-item label="来源单号" prop="sourcekey">
				<el-input v-model="dataForm.sourcekey" placeholder="来源单号" disabled></el-input>
			</el-form-item>

			<el-form-item label="来源单类型" prop="sourcetype">
				<el-input v-model="dataForm.sourcetype" placeholder="来源单类型" disabled></el-input>
			</el-form-item>

			<el-form-item label="来源单时间" prop="sourcedate">
				<el-input v-model="dataForm.sourcedate" placeholder="来源单时间" disabled></el-input>
			</el-form-item>

			<el-form-item label="关联单据" prop="relatedorderkey">
				<el-input v-model="dataForm.relatedorderkey" placeholder="关联单据" disabled></el-input>
			</el-form-item>

			<el-form-item label="关联单据行" prop="relatedorderline">
				<el-input v-model="dataForm.relatedorderline" placeholder="关联单据行" disabled></el-input>
			</el-form-item>

		</el-form>
		<div style="margin-top: 40px;font-size: 18px" v-show="isuniqueVisible">
			<span>唯一码管理:</span>
		</div>
		<div style="margin-top: 15px">
			<el-form :inline="true" v-show="isuniqueVisible">
				<el-form-item label="物料编码" prop="sku">
					<el-input v-bind:model-value="dataForm.sku" placeholder="物料编码" disabled></el-input>
				</el-form-item>
				<el-form-item label="物料名称" prop="descr">
					<el-input v-bind:model-value="dataForm.descr" placeholder="物料名称" disabled></el-input>
				</el-form-item>
				<el-form-item label="唯一码" prop="uniqueCode">
					<el-input v-model="message" placeholder="唯一码" @change="putMessage"></el-input>
				</el-form-item>
				<el-form-item label="已输入的唯一码">
					<el-input type="textarea" :autosize="{ minRows: 8, maxRows: 20}" placeholder="已输入的唯一码"
						style="width: 500px" v-model="dataForm.uniquecode">
					</el-input>
				</el-form-item>
			</el-form>
		</div>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import {
		ref,
		reactive,
		onMounted,
		nextTick,
		computed
	} from 'vue';
	import {
		getReceiptdetailById,
		putReceiptdetail,
		postReceiptdetail
	} from "@/network/lswms/receipt/Receiptdetail"
	import {
		getReceiptdetaillistById,
		putReceiptdetaillist,
		postReceiptdetaillist
	} from "@/network/lswms/receipt/Receiptdetaillist"
	import {
		ElMessage
	} from 'element-plus'
	import {
		getCodeListByCode,
	} from "@/network/lswms/base/Codelist.js"

	import _ from 'lodash'


	export default {
		name: "receiptdetaillistAddOrUpdate",
		emits: ["refreshDataList"],
		setup(props, {
			emit
		}) {
			const visible = ref(false);
			const handleType = ref('receipt');
			const dataForm = ref({
				id: '',
				receiptkey: '',
				line: '',
				linelist: '',
				sku: '',
				descr: '',
				unit: '',
				pack: '',
				status: '',
				qty: '',
				receiptqty: '',
				actqty: '',
				toloc: '',
				totray: '',
				tobox: "B01",
				lot: '',
				lot1: '',
				lot2: '',
				lot3: '',
				lot4: '',
				lot5: '',
				lot6: '',
				lot7: '',
				lot8: '',
				lot9: '',
				lot10: '',
				lot11: '',
				lot12: '',
				lot13: '',
				lot14: '',
				lot15: '',
				remark: '',
				sourcekey: '',
				sourcetype: '',
				sourcedate: '',
				relatedorderkey: '',
				relatedorderline: '',
				backstatus: '',
				backdate: '',
				backresult: '',
				backcode: '',
				warehouse: '',
				adduser: '',
				addtime: '',
				updatuser: '',
				updatetime: '',
				isuniquecode: '',
				uniquecode: '',
				companyid: '',
				companyname: '',
				price: 0,
				isunique: ''
			});
			onMounted(() => {
				dataForm.value.tobox = 'B01';
			})

			const dataRule = reactive({
				receiptkey: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				line: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				/* linelist: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ], */
				sku: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				// unit: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// pack: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				status: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				qty: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				actqty: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				toloc: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				totray: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				tobox: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],

			})

			const dataFormRef = ref(null)

			const init = () => {
				visible.value = true;

				nextTick(function() {
					dataFormRef.value.resetFields();
					boxsFn();
					receiptStatusFn();
					yesornoFn();
					newandoldsFn();
					dataForm.value.uniquecode = ''
					if (handleType.value == 'receipt') {
						getreceiptdetailInfo();
					} else {
						getreceiptdetaillistInfo();
					}

				})
			}

			const message = ref('')
			const putMessage = () => {
				if (message.value == '') {
					return;
				}
				let uniquecodes = dataForm.value.uniquecode.split(',')
				let newArr = uniquecodes.filter(function(value, index, array) {
					return value == message.value;
				});

				if (newArr.length == 0) {
					if (dataForm.value.uniquecode.length == 0) {
						dataForm.value.uniquecode = message.value
					} else {
						dataForm.value.uniquecode = dataForm.value.uniquecode + '\n' + message.value
					}
				}
				message.value = ''
			}
			
			
			const receiptStatus = ref("");

			const receiptStatusFn = () => {
				getCodeListByCode('receiptstatus').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					receiptStatus.value = res.data;
				}).catch((err) => {});
			}

			const getreceiptdetaillistInfo = () => {
				getReceiptdetaillistById(dataForm.value.id).then(function(res) {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					dataForm.value = _.assignIn({}, dataForm.value, res.data);
				}).catch((err) => {});
			}

			const yesornos = ref("");
			const yesornoFn = () => {
				getCodeListByCode('YESORNO').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					yesornos.value = res.data;
				}).catch((err) => {});
			}

			const newandolds = ref("");
			const newandoldsFn = () => {
				getCodeListByCode('NEWANDOLD').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					newandolds.value = res.data;
				}).catch((err) => {});
			}

			//获取明细行的记录
			const getreceiptdetailInfo = () => {

				getReceiptdetailById(dataForm.value.id).then(function(res) {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}

					dataForm.value = _.assignIn({}, dataForm.value, res.data);
					dataForm.value.tobox = "B01";
					dataForm.value.toloc = "PA";
					dataForm.value.actqty = 0;
					dataForm.value.lot4 = dataForm.value.price
					if (dataForm.value.isunique == '1') {
						isuniqueVisible.value = true
					}
					if (dataForm.value.isunique == '0') {
						isuniqueVisible.value = false
					}
				}).catch((err) => {});
			}

			// 表单提交
			const dataFormSubmitHandle = _.debounce(() => {
				dataFormRef.value.validate(function(valid) {
					if (!valid) {
						return false;
					}

					let qing = () => {
						return handleType.value == 'receipt' ? postReceiptdetaillist(dataForm.value) :
							putReceiptdetaillist(dataForm.value)
					}
					qing().then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								visible.value = false
								emit('refreshDataList')
							}
						})
					}).catch(function() {});

				})
			}, 1000, {
				'leading': true,
				'trailing': false
			})

			const boxs = ref("");
			const boxsFn = () => {

				getCodeListByCode('BOXLIST').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					boxs.value = res.data;
				}).catch((err) => {});
			}

			const isuniqueVisible = ref(false)


			return {
				isuniqueVisible,
				visible,
				dataForm,
				dataRule,
				init,
				dataFormSubmitHandle,
				dataFormRef,
				handleType,
				boxs,
				receiptStatus,
				putMessage,
				message,
				yesornos,
				newandolds

			}
		},
		methods: {
			//hasPermission
			reString: function(val) {
				let n = Number(val)
				if (!isNaN(n)) {
					return n
				} else {
					return val
				}
			}
		}

	}
</script>

<style scoped>

</style>
